﻿<cfscript>
/** 
* Captions Object service
* 
* @hint "Captions Object service" 
*/ 

component 
	displayname="public.service.util.captionsAdvice" 
	output=false 
	{
	
	/**
	* @hint "初始化对象"
	*/
	public public.service.util.captionsAdvice function init() output=false {
		return this;
	}

	/**
	* @hint "处理 Captions 对象转换为 SMPTE-TT 格式字幕"
	* 
	* @captions "字幕数组化对象"
	* @allowHTML "使用允许HTML标签"
	*/
	public string function getSMPTETT( required array captions, boolean allowHTML=false ) output=false {
		var _captions = captions;
		
		var srtString = createObject('java','java.lang.StringBuffer').init("");
		
		srtString.append('<?xml version="1.0" encoding="utf-8"?>' & chr(10) & chr(13));
		srtString.append('<tt xml:lang="en" xmlns:tts="http://www.w3.org/2006/10/ttaf1##styling" xmlns:ttm="http://www.w3.org/2006/10/ttaf1##metadata" xmlns:smpte="http://www.smpte-ra.org/schemas/2052-1/2010/smpte-tt" xmlns:m608="http://www.smpte-ra.org/schemas/2052-1/2010/smpte-tt##cea608">' & chr(10) & chr(13));
		srtString.append('<head>' & chr(10) & chr(13));
		srtString.append('<ttm:title>Fayea Subrip Converter</ttm:title>' & chr(10) & chr(13));
		srtString.append('<ttm:desc>converted document; </ttm:desc>' & chr(10) & chr(13));
		srtString.append('<ttm:copyright>Copyright (C) Fayea Studio 1999-#year(now())#.</ttm:copyright>' & chr(10) & chr(13));
		srtString.append('<styling>' & chr(10) & chr(13));
		srtString.append('<style xml:id="basic" tts:color="white" tts:fontFamily="Microsoft YaHei" tts:lineHeight="8%" tts:fontSize="8%" tts:fontWeight="normal" tts:textOutline="black 8% 8%" />' & chr(10) & chr(13));
		srtString.append('</styling>' & chr(10) & chr(13));
		srtString.append('<layout>' & chr(10) & chr(13));
		srtString.append('<region xml:id="pop" tts:backgroundColor="transparent"></region>' & chr(10) & chr(13));
		srtString.append('</layout>' & chr(10) & chr(13));
		srtString.append('</head>' & chr(10) & chr(13));
		srtString.append('<body>' & chr(10) & chr(13));
		srtString.append('<div>' & chr(10) & chr(13));

		for ( var i=1; i lte arrayLen(_captions); i++ ) {
			
			srtString.append('<p region="pop" style="basic" xml:space="preserve" begin="' & timeFormat(replace(_captions[i]['begin'], ',','.'), "HH:MM:SS") & '" end="' & timeFormat(replace(_captions[i]['end'], ',','.'), "HH:MM:SS") & '" tts:origin="15% 85%">'& chr(10) & chr(13));
			
			if (arguments.allowHTML) {
				srtString.append('<span><![CDATA[' & _captions[i]['text'] & ']]></span>'& chr(10) & chr(13));
			}
			else {
				srtString.append('<span><![CDATA[' & rereplace(_captions[i]['text'], "<[^>]*>", "","all") & ']]></span>'& chr(10) & chr(13));
			}
			
			srtString.append('</p>'& chr(10) & chr(13));
		}

		srtString.append('</div>' & chr(10) & chr(13));
		srtString.append('</body>' & chr(10) & chr(13));
		srtString.append('</tt>');
		
		return srtString.toString();
	}

	/**
	* @hint "处理 Captions 对象转换为 SRT 格式字幕"
	* 
	* @captions "字幕数组化对象"
	* @allowHTML "使用允许HTML标签"
	*/
	public string function getSrt( required array captions, boolean allowHTML=false ) output=false {
		
		var _captions = captions;
		
		var srtString = createObject('java','java.lang.StringBuffer').init("");
		
		for ( var i=1; i lte arrayLen(_captions); i++ ) {
			
			srtString.append( i & chr(10) & chr(13) );
			
			srtString.append( _captions[i]['begin'] & " --> " & _captions[i]['end'] & chr(10) & chr(13) );
			
			if (arguments.allowHTML) {
				srtString.append( _captions[i]['text'] );
			}
			else {
				srtString.append( rereplace(_captions[i]['text'], "<[^>]*>", "","all") );
			}
			
			if ( i lt arrayLen(_captions) ) {
				srtString.append( chr(10) & chr(13) & chr(10) & chr(13) );
			}
			
		}
		
		return srtString.toString();
		
	}
	
	/**
	* @hint "处理 SRT 字幕并转换成 Captions 对象"
	* 
	* @srtSource "将 SRT 转换为数组对象"
	*/
	public array function parseSrt( required string srtSource ) output=false {

		var _captions = [];

		var srtData = javaCast("String", srtSource);
		var srtList = [];
		
		srtData = srtData.replace("^\s+", '').replace("\s+$", '');
		srtList = srtData.split("\r\n\r\n");
		
		if( arrayLen(srtList) eq 1) { srtList = srtData.split("\n\n"); }
		
		for ( var i=1; i lte arrayLen(srtList); i++ ) {
			
			var _entity = {'begin'="", 'end'="", 'text'=""};

			/** Parse a single captions entry. **/
			var entityData = javaCast("String", srtList[i]);

			var entityArray = entityData.split("\r\n");
			if( arrayLen(entityArray) == 1) { entityArray = entityData.split("\n"); }
			
			try {
				
				// Second line contains the start and end.
				index = find(" --> ", entityArray[2]);
				
				if ( index gt 0 ) {
					
					_entity['begin'] = mid(entityArray[2], 1, index);
					_entity['end'] = mid(entityArray[2], index + 5, 12);
										
				}
				
				// Third line starts the text.
				if ( len(entityArray[3]) ) {
					
					_entity['text'] = entityArray[3];
					
					// Arbitrary number of additional lines.
					for ( var j=4; j lte arrayLen(entityArray); j++ ) {
						
						if ( len(entityArray[j]) ) {
							_entity['text'] =  listAppend(_entity['text'], entityArray[j], chr(10)&chr(13));
						}
						
					}
					
				}
				
				

			}
			catch (any excpt) {}
			
			if ( len(_entity['text']) ) {
				arrayAppend(_captions, _entity);
			}

		}
		
		return _captions;
	}

}

</cfscript>